<template>
  <div>
    <h3>
      <router-link to="/business/course">{{course.name}}</router-link>
    </h3>
    <p>
      <router-link to="/business/course" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-archive"></i>
        返回课程
      </router-link>
      &nbsp;
      <button @click="add()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa glyphicon-plus"></i>
        新增
      </button>
      &nbsp;
      <button @click="list()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>
    <Pagination ref="pagination" :list="list"></Pagination>
    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>
        <th>ID</th>
        <th>名称</th>
        <th>课程ID</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="chapter in chapters">
        <td>{{ chapter.id }}</td>
        <td>{{ chapter.name }}</td>
        <td>{{ chapter.courseId }}</td>
        <td>
          <div class="btn-group">
            <button  class="btn btn-xs btn-info" @click="toSection(chapter)">
              <i class="ace-icon fa fa-pencil bigger-120"></i>小结
            </button>
            <button class="btn btn-xs btn-info" @click="edit(chapter)">
              <i class="ace-icon fa fa-pencil bigger-120"></i>新增
            </button>

            <button class="btn btn-xs btn-danger">
              <i class="ace-icon fa fa-trash-o bigger-120" @click="del(chapter.id)"></i>删除
            </button>
          </div>
        </td>
      </tr>
      </tbody>
    </table>
    <!-- 弹窗 -->
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                  <input v-model="chapter.name" type="text" class="form-control" placeholder="名称">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">课程ID</label>
                <div class="col-sm-10">
                  <p class="form-control-static" >{{chapter.courseId}}</p>
<!--                  <input v-model="chapter.courseId" type="text" class="form-control" placeholder="课程ID">-->
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" @click="save">保存</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
  </div></template>

<script>
import Pagination from "@/components/pagination";

export default {
  components: {Pagination},
  name: "chapter",
  data() {
    return {
      chapter: {},
      chapters: [],
      course: {}
    }
  },
  props: {
    'activeSidebar': {
      type: Function
    }
  },
  mounted() {
    let _this = this;
    this.activeSidebar("business-course-sidebar")
    let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
    if (Tool.isEmpty(course)) {
      _this.$router.push("/welcome")
    }
    _this.course = course;
    _this.list(1);
  },
  methods: {
    list(page) {
      Loading.show();
      let _this = this;
      _this.$post(process.env.VUE_APP_SERVER + "/business/admin/chapter/list", {
        page: page,
        size: _this.$refs.pagination.size,
        courseId : _this.course.id
      }).then(res => {
        Loading.hide();
        console.log(res);
        let content = res.content;
        _this.chapters = content.list;
        _this.$refs.pagination.render(page, content.total);
      }).catch(res => {
        console.log(res);
      });
    },
    add() {
      let _this = this;
      $("#form-modal").modal('show');
    },
    save() {
      let _this = this;
      // 保存校验
      if (!Validator.require(_this.chapter.name, "名称")
          || !Validator.length(_this.chapter.courseId, "课程ID", 1, 8)) {
        return;
      }
      _this.chapter.courseId = _this.course.id;
      Loading.show();
      _this.$post(process.env.VUE_APP_SERVER + "/business/admin/chapter/save", _this.chapter).then(res => {
        Loading.hide();
        console.log(res);
        if (res.success) {
          $("#form-modal").modal('hide');
          _this.list(1);
          Toast.success('保存成功！');
        } else {
          $("#form-modal").modal('hide');
          _this.list(1);
          Toast.success(res.message);
        }
      }).catch(res => {
        console.log(res);
      });
    },
    del(id) {
      let _this = this;
      Confirm.show("删除大章后不可恢复!", function () {
        Loading.show();
        _this.$get(process.env.VUE_APP_SERVER + "/business/admin/chapter/del/" + id).then(res => {
          Loading.hide();
          console.log(res);
          if (res.success) {
            Toast.success('删除成功！');
            _this.list(1);
          } else {
            Toast.success('删除失败！');
            _this.list(1);
          }
        }).catch(res => {
          console.log(res);
          Toast.error('删除失败！')
        });
      })
    },
    edit(chapter) {
      let _this = this;
      _this.chapter = $.extend({}, chapter);
      $("#form-modal").modal('show');
    },
    // 点击[大章]
    toSection(chapter) {
      let _this = this;
      SessionStorage.set(SESSION_KEY_CHAPTER, chapter);
      _this.$router.push("/business/section")
    }
  }
}
</script>

<style scoped>

</style>